//
//  ZSY_HallViewController.m
//  ZSY_BreadTrip
//
//  Created by 张诗宇 on 15/12/28.
//  Copyright © 2015年 张诗宇. All rights reserved.
//

#import "ZSY_HallViewController.h"
#import "ZSY_NearbyModel.h"
#import "ZSY_HallTableViewCell.h"
#import "ZSY_NearbyDetailViewController.h"

@interface ZSY_HallViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) ZSY_BaseTableView *myTableView;
@property (nonatomic, retain) NSMutableArray *allArray;
@property(nonatomic,assign)NSInteger nextPage;//记录下一页
@property(nonatomic,assign)BOOL isUpLoading;//标记上拉操作还是下拉操作，yes为上拉

@end

@implementation ZSY_HallViewController

- (void)dealloc {
    [_allArray release];
    [_myTableView release];
    [super dealloc];
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor  = [UIColor blueColor];
    
    
    self.allArray = [NSMutableArray array];
    self.myTableView = [[ZSY_BaseTableView alloc] initWithFrame:[[UIScreen mainScreen] bounds] style:UITableViewStylePlain];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    // 取消cell 的分割线
    self.myTableView.separatorStyle = NO;
    [self.view addSubview:_myTableView];
    

    
    self.isUpLoading = NO;
    
    // 2.集成上拉下拉功能
    [self addHeader];
    [self addFooter];
    
    
    
    [_myTableView release];
}

#pragma mark --下拉刷新
- (void)addHeader
{
    //    __unsafe_unretained typeof(self) vc = self;
    
    __block ZSY_HallViewController *vc = self;//因为block块里用self会报警告,所以需要将self声明成block变量
    
    // 添加下拉刷新头部控件
    [self.myTableView addHeaderWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage = 0;//记录页码的
        vc.isUpLoading = NO;//标记为下拉操作
        [vc getDataList:vc.nextPage];//重新请求数据
        
    }];
    
#pragma mark 自动刷新(一进入程序就下拉刷新)
    [self.myTableView headerBeginRefreshing];
    
}

#pragma mark --上拉加载更多
- (void)addFooter
{
    //    __unsafe_unretained typeof(self) vc = self;
    __block ZSY_HallViewController *vc = self;
    
    // 添加上拉刷新尾部控件
    [self.myTableView addFooterWithCallback:^{
        // 进入刷新状态就会回调这个Block
        
        vc.nextPage += 20;
        vc.isUpLoading = YES;//标记为上拉操作
        [vc getDataList:vc.nextPage];//请求数据
        
    }];
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *indentifier = @"cell";
    ZSY_HallTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentifier];
    if (!cell) {
        cell = [[[ZSY_HallTableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:indentifier] autorelease];
    }
    cell.nearby = [_allArray objectAtIndex:indexPath.row];
    return cell;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return _allArray.count;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return 130;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    ZSY_NearbyDetailViewController *vc = [[ZSY_NearbyDetailViewController alloc] init];
    ZSY_NearbyModel *model = [_allArray objectAtIndex:indexPath.row];
    vc.model = model;
    [self.navigationController pushViewController:vc animated:YES];
    [vc release];
}

#pragma 用AFN请求数据
- (void)getDataList:(NSInteger)next {
    NSString *url = [NSString stringWithFormat:@"http://api.breadtrip.com/place/pois/nearby/?keyword=&category=5&start=%ld&count=20&latitude=38.883595&longitude=121.544665&sign=37a687b2acf2ba1b506a7f6ac1a61b55", next];
    [ZSY_AppTools creatingDataTask:url parameters:nil Cookie:nil User_Agent:nil successAFNBlock:^(id resuposeObject) {
        NSDictionary *result = resuposeObject;
        NSArray *itemArray = [result objectForKey:@"items"];
        for (NSDictionary *dic in itemArray) {
            ZSY_NearbyModel *model = [[ZSY_NearbyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
            [model release];
            NSLog(@"%ld", _allArray.count);
        }
        [self.myTableView footerEndRefreshing];
        [self.myTableView headerEndRefreshing];
        
        [self.myTableView reloadData];
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
